package com.langsin.liuwenqiu;


import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.liuwenqiu.mapper.GoodsMapper;
import com.langsin.liuwenqiu.mapper.TeacherMapper;
import com.langsin.liuwenqiu.mapper.WorkerMapper;
import com.langsin.liuwenqiu.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class Mytest {
    SqlSession sqlSession = null;
    /*
    在test方法执行之前执行
     */
    @Before
    public void before() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话(连接) 默认不自动提交
        sqlSession = sqlSessionFactory.openSession();
    }

    @After
    public void after() {
        sqlSession.commit();
        sqlSession.close();
    }

    /*
    查询worker表中的数据
     */
    @Test
    public void testMapper1() throws Exception {
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        List<Worker> workerList = workerMapper.selectByAge(50);
        workerList.forEach(System.out::println);
    }

    /*
    插入一条语句，此处选择雪花算法
     */
    @Test
    public void testMapper2() throws Exception {
        Worker worker = new Worker();
        // 主键 整数自增, 字符串uuid , 雪花算法
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("律师");
        worker.setAge(40);
        worker.setSex("女");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker(worker);
        System.out.println(row);
    }
    /*
    使用sql的uuid()方法生成一个随机的id
     */
    @Test
    public void testMapper3() throws Exception{
        Worker worker=new Worker();
        worker.setWname("医生");
        worker.setAge(55);
        worker.setSex("男");
        WorkerMapper workerMapper=sqlSession.getMapper(WorkerMapper.class);
        int row=workerMapper.insertWorker2(worker);
        System.out.println(row);
    }
    /*
    添加一条数据，并且在插入之后获得该数据新增的id值 此id值是在mysql表中已经设置了主键自动递增。
     */
    @Test
    public void addGoods(){
        Goods  goods=new Goods();
        goods.setGoodsName("平板");
        goods.setShopPrice(4598.0);
        GoodsMapper goodsMapper=sqlSession.getMapper(GoodsMapper.class);
        int row=goodsMapper.addGoods(goods);
        System.out.println(goods+","+row);
    }

    /*
    区分#{}和${}
     */
    @Test
    public void quertLike(){
//        String name="%A%";
        String name="A";

        GoodsMapper goodsMapper=sqlSession.getMapper(GoodsMapper.class);
//        List<Goods> goods = goodsMapper.queryByName(name);
        List<Goods> goods = goodsMapper.queryByName2(name);

        for (Goods good : goods) {
            System.out.println(good);
        }

    }

    /*
    使用${}测试代替表名的例子
     */
    @Test
    public void testTabname() throws Exception {
        String tabName = "goods";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tabName);
        System.out.println(tabName + "=" + counts);
    }


    @Test
    public void testUser() throws Exception{
        Integer uid = 702;
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);
    }

    @Test
    public void testVO() throws Exception{

        QueryVo vo = new QueryVo();
        vo.setRoleCode("manager");
        vo.setAddress("济南浪曦");

       UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryByRoleAndAddress(vo);

        users.forEach(System.out::println);
    }

    @Test
    public void testMap() throws Exception{
        HashMap<String,Object> map = new HashMap<>();
        map.put("roleCode", "plain");
        map.put("address", "济南浪曦");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.queryUserByMap(map);

        users.forEach(System.out::println);
    }

    /*
    注意数据库名和java属性名不一致的情况
     */
    @Test
    public void testTea() throws Exception{
        Integer tid = 1;
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.queryByID(tid);
        System.out.println(teacher);

    }

    @Test
    public void test() throws Exception {

        //查询一个worker “68c8d109-5e5d-11e9-9dc0-28d244d68b7c”
//        Worker worker = sqlSession.selectOne("worker.selectWorker","68c8d109-5e5d-11e9-9dc0-28d244d68b7c");
//
//        System.out.println(worker);

        //修改
        Worker worker = new Worker();
        worker.setAge(200);
        worker.setWid("4792a048-5e5d-11e9-9dc0-28d244d68b7c");
        int row = sqlSession.update("worker.updateAgeByID", worker);
        System.out.println(row);
    }

}
